<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :width="updateCoverImgShow ? '1000px' : '1000px'"
      :close-on-click-modal="false"
      @close="resetForm('form')"
    >
      <div style="min-height: 400px">
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-row :gutter="20">
            <CoverImg
              v-if="dialogVisible && updateCoverImgShow"
              :defaultValue="coverImgInfo"
              :type="21"
              @e-coverImgId="setCoverImgId"
            />
            <el-col
              :span="updateCoverImgShow ? 14 : 24"
              v-show="form.type == 2"
            >
              <el-form-item label="父节点" prop="pid">
                <el-select v-model="form.pid" filterable placeholder="请选择">
                  <el-option
                    v-for="v in data"
                    :key="v.id"
                    :label="v.name"
                    :value="v.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col
              :span="updateCoverImgShow ? 14 : 24"
              v-show="form.type == 3"
            >
              <el-form-item label="父节点" prop="pid">
                <ParendSelect
                  v-if="data.length && dialogVisible"
                  :options="data"
                  :defaultValue="form.pid"
                  @emit="setPid"
                />
              </el-form-item>
            </el-col>
            <el-col :span="updateCoverImgShow ? 14 : 24">
              <el-form-item label="名称" prop="name">
                <el-input v-model.trim="form.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="updateCoverImgShow ? 14 : 24">
              <el-form-item label="编码">
                <el-input v-model.trim="form.code"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="updateCoverImgShow ? 14 : 24">
              <el-form-item label="序号" prop="orderBy">
                <el-input-number
                  v-model="form.orderBy"
                  :min="1"
                  :max="1000"
                  size="medium"
                  label="序号"
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio label="0">展示</el-radio>
              <el-radio label="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="内涵描述" v-show="form.type == 3">
            <UEditor ref="ueditor" :defaultMsg="form.content" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  sysIdeologicalEducationAdd,
  sysIdeologicalEducationUpdate,
} from '@/api/admin/system/sysIdeologicalEducation.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    CoverImg: () => import('@/components/coverImg'),
    ParendSelect: () => import('@/components/educationSelect/parendSelect.vue'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      updateCoverImgShow: false,
      checkedData: {},
      nodeData: {},
      addOrEdit: 1,
      coverImgInfo: null,
      form: {
        pid: '0',
        name: '',
        code: '',
        fileId: '',
        orderBy: '10',
        status: '0',
        content: '',
        type: 1,
        level: 1,
        auditStatus: 2,
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
    }
  },
  props: ['data'],
  mounted() {
    // console.log(this.data);
    Bus.$on('showAddDialog_TreeAdd', (v) => {
      this.coverImgInfo = null
      this.nodeData = v.data ? JSON.parse(JSON.stringify(v.data)) : {}
      if (v.type == 'add') {
        this.updateCoverImgShow =
          this.nodeData.hasOwnProperty('pid') && this.nodeData.pid == 1
        this.handleAddOrEdit(1)
      } else {
        this.updateCoverImgShow =
          this.nodeData.hasOwnProperty('level') && this.nodeData.level == 2
        this.checkedData = v.data
        this.handleAddOrEdit(2)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showAddDialog_TreeAdd')
  },
  methods: {
    setPid(v) {
      this.form.pid = v
    },
    setCoverImgId(v) {
      this.form.fileId = v
    },
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.coverImgInfo = this.checkedData.hasOwnProperty('sysAttachment')
          ? this.checkedData.sysAttachment
          : ''
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        if (this.nodeData.hasOwnProperty('type')) {
          this.form.pid = this.nodeData.id
          this.form.type = ++this.nodeData.type
          this.form.level = ++this.nodeData.level
        } else {
          this.form.pid = 0
          this.form.type = 1
          this.form.level = 1
        }
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    //提交
    submitForm(form) {
      this.form.content = this.$refs.ueditor.message
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      sysIdeologicalEducationAdd(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false
          // const newChild = res.data.data[0];
          // if (!this.nodeData.childList) {
          //   this.$set(this.nodeData, "childList", []);
          // }
          // this.nodeData.childList.push(newChild);
          this.form.type == 3
            ? Bus.$emit('refreshList_EducationContent', { pid: this.form.pid })
            : Bus.$emit('refreshList_EducationTree')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    },
    //编辑
    edit() {
      sysIdeologicalEducationUpdate(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false
          this.form.type == 3
            ? Bus.$emit('refreshList_EducationContent')
            : Bus.$emit('refreshList_EducationTree')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.nodeData = {}
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.form.pid = '0'
      this.form.orderBy = '10'
      this.form.status = '0'
      this.form.type = 1
      this.form.level = 1
      this.form.auditStatus = 2
    },
  },
}
</script>